package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 物联网实名制认证补录环节实名制认证标签必填
 */

@Service
public class RealNameLableHandler extends AbstractAttrHandler {
    @Autowired
    private IQuerySceneInstService querySceneInstService;
    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;
    @Override
    public void init(CommonAttrVO commonAttrVO) {
        String sceneInstId = KiteWebContext.getContext().getCurrentSceneInstId();
        List<KiteProdInst> listProdInsts = querySceneInstService.listProdInsts(sceneInstId);
        //智能烟感和车载服务有多款，但只有个别款是非必填，其他是必填，但是配置时候是按照商品分类编码配置的，都是非必填，所以在此做个必填的校验
        List<KiteProdInst> isNotNullProduct = listProdInsts.stream().filter(KiteProdInst->KiteStringUtils.equalsAny(KiteProdInst.getSpecId(),"30000003","30000021","30000010")).collect(Collectors.toList());
        String taskCode = querySceneInstService.getExtParameterValue(sceneInstId, "taskCode");
        //如果是补录环节，则必填
        if(KiteStringUtils.equals(taskCode, "OrderAddRecordWLWXS")){
            commonAttrVO.setIsNull(KeyConsts.IFTRUE_F);
        }else if(KiteListUtils.isNotEmpty(isNotNullProduct)&& isNotNullProduct.size()>0){
            //如果不是补录环节，如果不是雁飞智能烟感或Cat1 OBD服务，则必填
            commonAttrVO.setIsNull(KeyConsts.IFTRUE_F);
        }

    }

    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        List<KiteProdInst> listProdInsts = querySceneInstService.listProdInsts(sceneInstId);
        //智能烟感和车载服务有多款，但只有个别款是非必填，其他是必填，但是配置时候是按照商品分类编码配置的，都是非必填，所以在此做个必填的校验
        List<KiteProdInst> isNotNullProduct = listProdInsts.stream().filter(KiteProdInst->KiteStringUtils.equalsAny(KiteProdInst.getSpecId(),"30000003","30000021","30000010")).collect(Collectors.toList());
        String taskCode = querySceneInstService.getExtParameterValue(sceneInstId, "taskCode");
        //如果是补录环节，则全部必填
        KiteAttr realNameLableAttr = querySceneInstService
            .getCommonAttrsByFieldName(sceneInstId, "ORD10293", "2021113001");
        if(KiteStringUtils.equals(taskCode, "OrderAddRecordWLWXS")){
            realNameLableAttr.setIsNull(KeyConsts.IFTRUE_F);
        }else if(KiteListUtils.isNotEmpty(isNotNullProduct)&& isNotNullProduct.size()>0){
            //如果不是补录环节，如果不是雁飞智能烟感或Cat1 OBD服务，则必填
            realNameLableAttr.setIsNull(KeyConsts.IFTRUE_F);
        }

        operateSceneCommonService.setCommonAttr(sceneInstId, realNameLableAttr);
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }

}
